package com.ccnu.JobHub.controller;

import com.ccnu.JobHub.model.common.Result;
import com.ccnu.JobHub.service.impl.PostService;
import com.ccnu.JobHub.service.impl.StudentService;
import com.ccnu.JobHub.service.impl.StudentTagService;
import com.ccnu.JobHub.service.impl.TagForStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("analysis")
@CrossOrigin("*")
@Slf4j
public class AnalysisController {

    private final PostService postService;

    private final StudentService studentService;

    private final StudentTagService studentTagService;
    private final TagForStudentService tagForStudentService;

    public AnalysisController(PostService postService, StudentService studentService, StudentTagService studentTagService, TagForStudentService tagForStudentService) {
        this.postService = postService;
        this.studentService = studentService;
        this.studentTagService = studentTagService;
        this.tagForStudentService = tagForStudentService;
    }

    /**
     * 获取每日发帖量
     * @param n 过去的n天
     * @return 一个json数据里面包含过去n天每天的发帖量
     */
    @GetMapping("dailyPostingAmount")
    public Result dailyPostingAmount(@RequestParam("long") Integer n){
        log.info("--------------------获取每日发帖量请求--------------------");
        if (n<1){
            return Result.error("输入时间长度不正确");
        }
        LocalDate now = LocalDate.now();
        LocalDate startDate = now.minusDays(n); // 获取n天前的日期
        List<Map<String, Object>> amounts = postService.getPostCountByDateRange(startDate.toString(),now.toString());
        Map<String ,Object> result = new HashMap<>();
        for (int i = 1 ; i <= n; i++){
            result.put(now.minusDays(i).toString(),0);
        }
        for (Map<String ,Object> amount : amounts){
            result.put(amount.get("post_date").toString(),amount.get("amount"));
        }
        return Result.success("成功获取每日发帖数量",result);
    }

//    @GetMapping("dailyRegisterAmount")
//    public Result dailyRegisterAmount(@RequestParam("long") Integer n){
//        log.info("--------------------获取每日注册人数请求--------------------");
//        if (n<1){
//            return Result.error("输入时间长度不正确");
//        }
//        LocalDate now = LocalDate.now();
//        LocalDate startDate = now.minusDays(n); // 获取n天前的日期
//        List<Map<String, Object>> amounts = studentService.getCountByRegister(startDate.toString(),now.toString());
//        Map<String ,Object> result = new HashMap<>();
//        for (int i = 1 ; i <= n; i++){
//            result.put(now.minusDays(i).toString(),0);
//        }
//        for (Map<String ,Object> amount : amounts){
//            result.put(amount.get("post_date").toString(),amount.get("amount"));
//        }
//        return Result.success("成功获取每日发帖数量",result);
//    }

    /**
     * 获取男生和女生人数还有未设置性别的人数
     * @return 一个Result类型结果，包含：
     *     "MaleCount",
     *     "FemaleCount",
     *     "UnknownCount"
     */
    @GetMapping("maleAndFemaleCount")
    public Result maleAndFemaleCount(){
        log.info("--------------------获取男生和女生人数请求--------------------");
        Map<String,Object> result = studentService.getMaleAndFemaleCount();
        return Result.success("成功获取男生和女生人数",result);
    }

    /**
     * 获取认证人数和未认证人数
     * @return 一个result结果
     */
    @GetMapping("verifiedCount")
    public Result verifiedCount(){
        log.info("--------------------获取认证人数和未认证人数请求--------------------");
        Map<String,Object> result = studentService.getVerifiedCount();
        return Result.success("成功获取认证人数和未认证人数",result);
    }

    /**
     * 获取热门标签及其对应人数
     * @return 返回一个result结果
     */
    @GetMapping("tagCount")
    public Result TagCount(){
        List<Map<String ,Object>> list = studentTagService.getTagCount();
        Map<String ,Object> result = new HashMap<>();
        for (Map<String ,Object> a : list){
            String tagName = tagForStudentService.getById((Serializable) a.get("tagId")).getTagName();
            result.put(tagName,a.get("amount"));
        }
        return Result.success("成功获得热门标签及其对应人数",result);
    }

}
